package com.ruoyi.web.controller.bigScreen;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.WeChatDataInfo;
import com.ruoyi.common.core.page.WeChatTableDataInfo;
import com.ruoyi.datastatistics.domain.xbmaindata.MainMeterHeaderReadingRate;
import com.ruoyi.datastatistics.service.IMainService;
import com.ruoyi.datastatistics.service.IMainXbService;
import com.ruoyi.datastatistics.service.ITerminalsFreezeXbService;
import com.ruoyi.datastatistics.wxentity.WXTerminalsFreezeXb;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/BigScreen")
public class BigScreenController extends BaseController {

    @Autowired
    private IMainXbService mainXbService;

    @Autowired
    private ITerminalsFreezeXbService terminalsFreezeXbService;

    @Autowired
    private IMainService mainService;
    /**
     * 微信小程序首页，设备总数
     * @param
     * @return
     */
    @PostMapping("/ObtainDigitalDisplay")
    public WeChatDataInfo ObtainDigitalDisplay(String AreaIDArrStr)
    {
        Map<String, Object> obj = new HashMap<>();
       try
        {
            Map<String, Object> xb_num =  mainXbService.getMainHeadDataDb();

            Map<String, Object> db_num = mainService.getMainHeadDataDb();
            Integer equipmentNumber = Integer.parseInt(xb_num.get("terminalNum").toString());
            equipmentNumber = equipmentNumber + Integer.parseInt(db_num.get("totalCount").toString());
            Integer usersNumber = Integer.parseInt(xb_num.get("userNum").toString());
            usersNumber = usersNumber + Integer.parseInt(db_num.get("totalCount").toString());

            Integer onlineCount = Integer.parseInt(db_num.get("onlineCount").toString());
            MainMeterHeaderReadingRate readingRate= (MainMeterHeaderReadingRate) xb_num.get("meterReadingRate");
            onlineCount = onlineCount + readingRate.getMeterReadingNum();

            Double onlinerate = Double.parseDouble (onlineCount/equipmentNumber *100+"");
            obj.put("EquipmentNumber",equipmentNumber);
            obj.put("UsersNumber",usersNumber);
            obj.put("Onlinerate",onlinerate);
        }
        catch (Exception e)
        {
        }
        return getWeChatDataTableSUCCESS(obj);
    }
    /**
     * 小表日用量月用量数据
     */
    @PostMapping("/ObtainYesterdayDailyDosageSort")
    public WeChatTableDataInfo WxListOrderByFlow(@RequestParam(value = "startTime",required = false) String startTime,
                                                 @RequestParam(value = "endTime",required = false) String endTime,
                                                 @RequestParam(value = "reportType",required = false) String reportType,
                                                 @RequestParam(value = "userTerminalId",required = false)String terminalId)
    {
        PageDomain pageDomain = startWeChatPage();
        List<WXTerminalsFreezeXb> list = terminalsFreezeXbService.WxListOrderByFlow(startTime,endTime,reportType,terminalId);
        return getWeChatDataTable(list,pageDomain);
    }
}
